layui.use(['table','form','laydate','flow'],function () {
    var table=layui.table,
        form=layui.form,
        laydate=layui.laydate,
        flow=layui.flow;
    var $=layui.$;
    flow.load({
        elem: '#layFlow' //流加载容器
        ,done: function(page, next){ //执行下一页的回调
            $.ajax({
                type:'post',
                url:'/Ticket/ticketInfo/getPages',
                dataType: 'json',
                success:function (res) {
                    if (res.code==0)
                    {
                        var pages=res.count;
                        $.ajax({
                            type:'post',
                            url:'/Ticket/ticketInfo/get',
                            data: {'page':page},
                            dataType:'json',
                            success:function (res) {
                                if (res.code==0)
                                {
                                    var d=eval(res.data);
                                    //模拟数据插入
                                    setTimeout(function(){
                                        var lis = [];
                                        var num = 4;
                                        if (d.length < 4){
                                            num = d.length;
                                        }
                                        for(var i = 0; i <num; i++){
                                            lis.push('<form class="layui-form" action="" style="border: 1px red solid;width: 300px;">'+
                                                // '<span style="margin-left: 15px;">页码：'+page+'序号：'+(i+1)+'</span>'+'<span id="ticketId" name="ticketId" style="opacity: 0;">'+d[i].id+'</span>'+
                                                '<div class="layui-form-item" style="line-height: 20px;height: 20px;margin-top: 2px;">'+
                                                '<label class="layui-form-label" style="width: 100px;">页码：'+page+'序号：'+(i+1)+'</label><div class="layui-input-block" style="width: 150px;margin-left: 0px;height: 0px;float: right;"><input type="text" name="ticketId" lay-verify="title" class="layui-input" style="opacity: 0;height: 0px;" value="'+d[i].id+'"></div></div>'+
                                                '<div class="layui-form-item" style="line-height: 40px;height: 40px;margin-top: 2px;"><label class="layui-form-label lable">'+
                                                '<p style="position:relative;line-height: 20px;height: 20px;width:150px;"><span style="float: left;">车辆：</span><span style="color: springgreen;float: left;">'+d[i].carId+'</span></p><p style="position:relative;line-height: 20px;height: 20px;width:200px;"><span style="float: left;">发车时间：</span><span style="float: left;color: red;">'+formatDate(d[i].validTime)+'</span></p></label></div>'+
                                                '<div class="layui-form-item" style="line-height: 40px;height: 40px;margin-top: 2px;"><label class="layui-form-label lable">'+
                                                '<p style="position:relative;line-height: 20px;height: 20px;width:150px;"><span style="float: left;">车票类型：</span><span style="color: springgreen;float: left;">'+d[i].ticketType+'</span></p><p style="position:relative;line-height: 20px;height: 20px;width:150px;"><span style="float: left;">车票状态：</span><span style="float: left;color: red">'+d[i].type+'</span></p></label></div>'+
                                                '<div class="layui-form-item" style="line-height: 40px;height: 40px;margin-top: 2px;"><label class="layui-form-label lable">'+
                                                '<p style="position:relative;line-height: 20px;height: 20px;width:150px;"><span style="float: left;">乘车人：</span><span style="color: springgreen;float: left;">'+d[i].userid+'</span></p><p style="position:relative;line-height: 20px;height: 20px;width:150px;"><span style="float: left;">车票价格：</span><span style="float: left;color: red">'+d[i].price+'</span></p></label></div>'+
                                                '<div class="layui-form-item" style="line-height: 40px;height: 40px;margin-top: 2px;"><label class="layui-form-label lable">'+
                                                '<p style="position:relative;line-height: 20px;height: 20px;width:150px;"><span style="float: left;">检票口：</span><span style="color: springgreen;float: left;">'+d[i].checkTicket+'</span></p><p style="position:relative;line-height: 20px;height: 20px;width:150px;"><span style="float: left;">座位号：</span><span style="float: left;color: red">'+d[i].seatNum+'</span></p></label></div>'+
                                                '<div class="layui-form-item" style="line-height: 40px;height: 40px;margin-top: 2px;"><div class="layui-input-block">'+
                                                '<button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">退票</button></div></div></form>')
                                        }
                                        //执行下一页渲染，第二参数为：满足“加载更多”的条件，即后面仍有分页
                                        //pages为Ajax返回的总页数，只有当前页小于总页数的情况下，才会继续出现加载更多
                                        next(lis.join(''), page < pages); //假设总页数为 10
                                    }, 500);
                                }
                            }
                        });
                    }
                    else layer.msg("加载失败")
                }
            });
        }
    });
    //新增表单提交
    form.on('submit(submit)', function(data){
        layer.confirm("退票将扣除手续费！确定退票？",function (index) {
            $.ajax({
                type:'post',
                url:'/Ticket/ticketInfo/del',
                dataType:'json',
                data: {'ticketId':data.field.ticketId},
                success:function (res) {
                    if (res.code == 0) {
                        layer.open({
                            title: '提示'
                            ,content: res.msg,
                            time:3000
                        });
                        setTimeout(function () {
                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            location.reload();//新增成功后刷新当前界面
                        },3000);
                    }else layer.msg(res.msg);
                }
            })
            layer.close(index);
        })
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });
});
//时间戳转换为日期格式
function formatDate(time) {
    var now = new Date(time);
    var year=now.getFullYear();  //取得4位数的年份
    var month=now.getMonth()+1;  //取得日期中的月份，其中0表示1月，11表示12月
    var date=now.getDate();      //返回日期月份中的天数（1到31）
    return year+"-"+month+"-"+date;
}
